'use strict';

export default class extends think.service.base {
    /**
     * init
     * @return {}         []
     */

    init() {
        this.user = this.model('user', 'user');
    }

    async addweichat(obj, _url) {
        let httpService = think.service('http');
        let http = new httpService();
        console.log(obj.url + obj.APPID + '&redirect_uri=' + _url)
        http.set({
            url: obj.url + obj.APPID + '&redirect_uri=' + _url
        });
        await http.http((err, rsp, body) => {
            console.log(rsp)
            console.log(body)
            if (err) {
                throw err;
            }
        })
    }

    async getToken(config) {
        let httpService = think.service('http');
        let http = new httpService();
        let url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' + config.APPID + '&secret=' + config.APPSECRET + '&code=' + config.code + '&grant_type=authorization_code';
        var openID, access_token;
        http.set({
            method: 'GET',
            uri: url
        })
        return new Promise(function(resolve, reject) {
            http.http((err, rsp, body) => {
                resolve(body)
                if (err) {
                    reject(new Error());
                }
            })
        });

    }

    async getUserInfo(config) {
        let httpService = think.service('http');
        let http = new httpService();
        let url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' + config.access_token + '&openid=' + config.openid + '&lang=zh_CN';
        http.set({
            method: 'GET',
            uri: url
        })
        return new Promise(function(resolve, reject) {
            http.http((err, rsp, body) => {
                resolve(body)
                if (err) {
                    reject(new Error());
                }
            })
        });
    }

    async addUser(obj) {
        let model = {
            name: encodeURIComponent(obj.nickname),
            openid: obj.openid,
            img: obj.headimgurl,
            role_id: 10,
            active: 1
        }

        let userId = await this.user.addUser(model)
        return userId.id;
    }

    async getWechatViewHistory(id) {
        let redisService = think.service('redisservice');
        let redis = new redisService();
        let weChatUser = redis.getWeiUserInfo(id).then(function(data) {
            return data;
        });
        return weChatUser;
    }


    async getCount(name) {
        let redisService = think.service('redisservice');
        let redis = new redisService();
        let weChatUser = redis.get(name).then(function(data) {
            return data;
        });
        return weChatUser;
    }

    async addCount(name) {
        let redisService = think.service('redisservice');
        let redis = new redisService();
        let weChatUser = redis.add(name)
        return weChatUser;
    }

    async setClearTime(name,time) {
        let redisService = think.service('redisservice');
        let redis = new redisService();
        let weChatUser = redis.setClearTime(name,time)
        return weChatUser;
    }
}
